package net.plankton.controller;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class CommandHandler extends Thread {
	
	Socket socket;
	
	public CommandHandler(Socket s) {
		this.socket = s;
	}
	
	public void run() {
		try {
			InputStream in = socket.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(in));
			String line = reader.readLine();
			Protocol p=null;
			if (line!=null) {
				p = Controller.resolveProtocol(line.trim()).clone();
			}
			if (p!=null) {
				line = reader.readLine();
				while (line!=null) {
					p.command(line);
					line = reader.readLine();
				}
			}
			reader.close();
			in.close();
			socket.close();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
